<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2022-08-25 21:34:16
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Repositories;


use App\Models\FinanceAcceptanceRec;

class FinanceAcceptanceRecRepository extends Repositories
    {
    protected $relation_models = [];
    public function boot(FinanceAcceptanceRec $model)
        {
        $this->model = $model;
        }

    public function validate(&$data)
        {
        $buy_account_flage  = false;
        $buy_customer_flage = false;
        if (isset($data['customer_id']) && is_numeric($data['customer_id']) && $data['customer_id'] > 0) {
            $buy_customer_flage = true;
            }

        if (isset($data['buyaccount_id']) && is_numeric($data['buyaccount_id']) && $data['buyaccount_id'] > 0) {
            $buy_account_flage = true;
            }
        if (!isset($data['buyaccount_id']) || isEmpty($data['buyaccount_id']) || $data['buyaccount_id'] == 0) {
            $data['buyaccount_id'] = null;
            }
        if ($buy_account_flage) {
            if ($buy_customer_flage) {
                $data['type'] = '购';
                }
            else {
                fail("客购票，必填客户;应收票不填购票账户;银行借票，两个都不能填！");
                }
            }
        else {
            if ($buy_customer_flage) {
                $data['type'] = '收';
                }
            else {
                $data['type'] = '借';
                }
            }
        return $this;
        }


    //内转承兑
    protected function createtransfertoRec($data)
        {
        $gdata = [
            'code'                 => $data->codes,
            'type'                 => $data->type,
            'amount'               => $data->transfer_amount,
            'leftamount'           => $data->transfer_amount,
            'status_id'            => 0,
            'recedate'             => $data->recedate,
            'customer_id'          => $data->customer_id,
            'buyaccount_id'        => $data->buyaccount_id,
            'trade_id'             => $data->handletrade_id,
            'handletrade_billdate' => $data->handletrade_billdate,
            'billtrade_id'         => $data->billtrade_id,
            'handletrade_id'       => $data->trade_id,
            'bank'                 => $data->bank,
            'duedate'              => $data->duedate,
            'draftdate'            => $data->draftdate,
            'remark'               => $data->remark,
        ];
        FinanceAcceptanceRec::create($gdata);
        }

    }



